package com.orange.util.document;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.orange.engine.device.Device;
import com.orange.util.debug.Debug;

public class Document {
	static String documentPath = "document/";

	private static DocumentData getDocument(String pDocumentName) {
		String fileName = Device.getDevice().getFileManage().getAbsolutePathOnExternalStorage(documentPath + pDocumentName);
		File file = new File(fileName);
		if (!file.exists())
			return null;
		DocumentData mDocumentData = new DocumentData(file);
		if (mDocumentData.isLoad)
			return mDocumentData;
		else {
			mDocumentData.clean();
			mDocumentData = null;
			return null;
		}
	}

	private static boolean saveDocument(String pDocumentName, DocumentData pDocumentData) {

		String content = pDocumentData.getStringContent();
		FileOutputStream out = null;
		String fileName = Device.getDevice().getFileManage().getAbsolutePathOnExternalStorage(documentPath);
		File file = new File(fileName);
		if (!file.exists())
			file.mkdirs();
		fileName = Device.getDevice().getFileManage().getAbsolutePathOnExternalStorage(documentPath + pDocumentName);
		System.out.println(fileName);
		try {
			file = new File(fileName);
			if (!file.exists())
				file.createNewFile();
			out = new FileOutputStream(file);
			out.write(content.getBytes("utf-8"));
		} catch (Exception e) {
			if (Debug.DEBUG)
				Debug.e("saveDocument", e);
			return false;
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return true;
	}

	public static String getString(String pDocumentName, String pKey, String pDefault) {
		DocumentData mDocumentData = getDocument(pDocumentName);
		if (mDocumentData == null)
			return pDefault;
		Object obj = mDocumentData.getData(pKey);
		if (obj != null && obj instanceof String) {
			return (String) obj;
		} else {
			return pDefault;
		}
	}

	public static int getInt(String pDocumentName, String pKey, int pDefault) {
		DocumentData mDocumentData = getDocument(pDocumentName);
		if (mDocumentData == null)
			return pDefault;
		Object obj = mDocumentData.getData(pKey);
		if (obj != null && obj instanceof Integer) {
			return (Integer) obj;
		} else {
			return pDefault;
		}
	}

	public static long getLong(String pDocumentName, String pKey, long pDefault) {
		DocumentData mDocumentData = getDocument(pDocumentName);
		if (mDocumentData == null)
			return pDefault;
		Object obj = mDocumentData.getData(pKey);
		if (obj != null && obj instanceof Long) {
			return (Long) obj;
		} else {
			return pDefault;
		}
	}

	public static boolean getBoolean(String pDocumentName, String pKey, boolean pDefault) {
		DocumentData mDocumentData = getDocument(pDocumentName);
		if (mDocumentData == null)
			return pDefault;
		Object obj = mDocumentData.getData(pKey);
		if (obj != null && obj instanceof Boolean) {
			return (Boolean) obj;
		} else {
			return pDefault;
		}
	}

	public static boolean putString(String pDocumentName, String pKey, String pData) {
		if (pData.indexOf(",") != -1) {
			Debug.d("putString", "pString does not contain ','");
			return false;
		}
		boolean flag = false;
		DocumentData mDocumentData = getDocument(pDocumentName);
		if (mDocumentData != null) {
			flag = mDocumentData.setData(pKey, pData);
		} else {
			mDocumentData = new DocumentData();
			flag = mDocumentData.setData(pKey, pData);
		}
		if (flag)
			flag = saveDocument(pDocumentName, mDocumentData);
		return flag;
	}

	public static boolean putInt(String pDocumentName, String pKey, int pData) {
		boolean flag = false;
		DocumentData mDocumentData = getDocument(pDocumentName);
		if (mDocumentData != null) {
			flag = mDocumentData.setData(pKey, pData);
		} else {
			mDocumentData = new DocumentData();
			flag = mDocumentData.setData(pKey, pData);
		}
		if (flag)
			flag = saveDocument(pDocumentName, mDocumentData);
		return flag;
	}

	public static boolean putLong(String pDocumentName, String pKey, long pData) {
		boolean flag = false;
		DocumentData mDocumentData = getDocument(pDocumentName);
		if (mDocumentData != null) {
			flag = mDocumentData.setData(pKey, pData);
		} else {
			mDocumentData = new DocumentData();
			flag = mDocumentData.setData(pKey, pData);
		}
		if (flag)
			flag = saveDocument(pDocumentName, mDocumentData);
		return flag;
	}

	public static boolean putBoolean(String pDocumentName, String pKey, boolean pData) {
		boolean flag = false;
		DocumentData mDocumentData = getDocument(pDocumentName);
		if (mDocumentData != null) {
			flag = mDocumentData.setData(pKey, pData);
		} else {
			mDocumentData = new DocumentData();
			flag = mDocumentData.setData(pKey, pData);
		}
		if (flag)
			flag = saveDocument(pDocumentName, mDocumentData);
		return flag;
	}
}